#include <cstdio>
#include <cstdlib>

using namespace std;

int main() {
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);

    int n, m, x1, y1, x2, y2, ans=0;
    scanf("%d %d %d %d %d %d", &n, &m, &x1, &y1, &x2, &y2);
    if (abs(x1-x2)+abs(y1-y2)<=1) ans=-1;
    else if (x2<=2 && y2<=2 && n-x2<=1 && m-y2<=1) ans=1;
    else if (n<=2 || m<=2) {
        if (abs(x1-x2)<=1 && abs(y1-y2)<=1) ans=3;
        else ans=2;
    } else if ((x2==n-1 || x2==2) && (y2==m-1 || y2==2)) {
        if (abs(x1-x2)<=1 && abs(y1-y2)<=1 && x1>2 && y1>2 && x1<n-1 && y1<m-1) ans=3;
        else ans=2;
    } else ans=3;
    printf("%d\n", ans);

    fclose(stdin);
    fclose(stdout);
    return 0;
}
